package com.bigmi.admin.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigmi.admin.mapper.SensitiveMapper;
import com.bigmi.admin.service.SensitiveService;
import com.bigmi.apis.article.ArticleClientIp;
import com.bigmi.model.admin.dto.SensitiveDto;
import com.bigmi.model.common.dtos.PageResponseResult;
import com.bigmi.model.common.dtos.ResponseResult;
import com.bigmi.model.common.enums.AppHttpCodeEnum;
import com.bigmi.model.wemedia.pojos.WmSensitive;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {
    @Autowired
    private SensitiveMapper sensitiveMapper;



    @Override
    public ResponseResult listAll(SensitiveDto sensitiveDto) {
        if (sensitiveDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        sensitiveDto.checkParam();
        IPage page =new Page(sensitiveDto.getPage(),sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper=new LambdaQueryWrapper<>();
        //模糊查询
        if (StringUtils.isNotBlank(sensitiveDto.getName())){
            queryWrapper.like(WmSensitive::getSensitives,sensitiveDto.getName());
        }
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        page=page(page,queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        List records = page.getRecords();
        log.info(""+records);
        log.info(""+responseResult);
        return responseResult;
    }

    @Override
    public ResponseResult add(WmSensitive wmSensitive) {
        if (wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmSensitive sensitive =new WmSensitive();
        sensitive.setSensitives(wmSensitive.getSensitives());
        sensitive.setCreatedTime(new Date());
        save(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        sensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult upDate(WmSensitive wmSensitive) {
        if (wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        WmSensitive sensitive =new WmSensitive();
        sensitive.setId(wmSensitive.getId());
        sensitive.setSensitives(wmSensitive.getSensitives());
        sensitive.setCreatedTime(new Date());
        updateById(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
